package al.xc.redis;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.*;

import java.util.*;

@Slf4j
public class RedisConnectCluster implements IRedisConnect {

    /**
     * 连接池
     */
    JedisCluster cluster_ = null;

    /**
     * 初始化
     * @param config
     * @param host
     * @param port
     * @param timeout
     * @param password
     * @param index
     */
    @Override
    public void initialize(GenericObjectPoolConfig config, String[] host, int[] port, int timeout, String password, int index ) {
        Set<HostAndPort> nodes = new HashSet<>();
        List<JedisShardInfo> shareInfos = new ArrayList<>();
        for (int i = 0; i < host.length; ++i) {
            nodes.add(new HostAndPort(host[i], port[i]));
        }
        cluster_ = new JedisCluster(nodes, timeout, timeout, 5, password, config);
    }

    /**
     * 获取操作对象
     * @return
     */
    protected synchronized JedisCluster newConnect() {
        return cluster_;
    }

    /**
     * 获取数据
     * @param key
     * @param fields
     * @return
     */
    @Override
    public List<String> hmget(String key, String... fields) {
        JedisCluster connect = newConnect();
        List<String> result = connect.hmget(key, fields);
        return result;
    }

    /**
     * 获取数据
     * @param key
     * @param field
     * @return
     */
    @Override
    public String hget(String key, String field) {
        JedisCluster connect = newConnect();
        String result = connect.hget(key, field);
        return result;
    }



    /**
     * 获取所有数据
     *
     * @param key
     * @return
     */
    @Override
    public Map<String, String> hgetAll(String key) {
        JedisCluster connect = newConnect();
        return connect.hgetAll(key);
    }

    /**
     * 设置数据
     * @param key
     * @param field
     * @param vl
     * @return
     */
    @Override
    public Long hset(String key, String field, String vl) {
        JedisCluster connect = newConnect();
        return connect.hset(key, field, vl);
    }

    /**
     * 删除数据
     *
     * @param key
     * @param field
     * @return
     */
    @Override
    public Long hdel(String key, String... field) {
        JedisCluster connect = newConnect();
        return connect.hdel(key, field);
    }

    /**
     * 订阅频道
     * @param pubSub
     * @param channel
     */
    @Override
    public void subscribe(JedisPubSub pubSub, String... channel) {
        JedisCluster connect = newConnect();
        connect.subscribe(pubSub, channel);
    }

    /**
     * 订阅模式
     * @param pubSub
     * @param partten
     */
    @Override
    public void psubscribe(JedisPubSub pubSub, String... partten) {
        JedisCluster connect = newConnect();
        connect.psubscribe(pubSub, partten);
    }

    /**
     * 发布
     * @param channel
     * @param value
     */
    @Override
    public void publish(String channel, String value) {
        JedisCluster connect = newConnect();
        connect.publish(channel, value);
    }

    /**
     * 执行脚本
     * @param script
     * @param keys
     * @param args
     * @return
     */
    @Override
    public Object eval(final String script, final List<String> keys, final List<String> args) {
        JedisCluster connect = newConnect();
        return connect.eval(script, keys, args);
    }

    /**
     * 定位数据添加
     *
     * @param key
     * @param longitude
     * @param latitude
     * @param member
     * @return
     */
    @Override
    public Long geoadd(String key, double longitude, double latitude, String member) {
        JedisCluster connect = newConnect();
        return connect.geoadd(key, longitude, latitude, member);
    }

    /**
     * 删除集合元素
     * @param key
     * @param member
     * @return
     */
    @Override
    public Long zrem(String key, String... member) {
        JedisCluster connect = newConnect();
        return connect.zrem(key, member);
    }

    /**
     * 获取范围内的元素
     * @param key
     * @param longitude
     * @param latitude
     * @param radius
     * @param unit
     * @return
     */
    @Override
    public List<GeoRadiusResponse> georadius(String key, double longitude, double latitude, double radius, GeoUnit unit) {
        JedisCluster connect = newConnect();
        return connect.georadius(key, longitude, latitude, radius, unit);
    }
}

